<?php
namespace frontend\controllers;

use Yii;
use yii\web\Session;
use yii\web\Controller;
use yii\data\Pagination;
use backend\models\article\Comment;
use backend\models\system\SysConfig;
use backend\models\article\Article;
use backend\models\article\Artlable;
use frontend\models\operate\ArticleInfo;
use frontend\models\operate\User;
use backend\models\Collection;
	/**	
	 * @inheritdoc	模块：行业资讯
	 * @author Yang Zhang
	 */
class OperateController extends Controller
{

	/**
	 * @inderitdoc 验证码配置
	 */
	public function actions()
	{
		return [
			'captcha'=>[
				'class'=>'yii\captcha\CaptchaAction',
				'maxLength'=>5,
				'minLength'=>5,
			],
		];
	}
	/**
	 * @inheritdoc 公共部分
	 */

	public function commen()
	{
		/*热门文章*/
        $NewArticle= new Article;
        $NewArticle = $NewArticle->PopularArticles();
        /*热门标签*/
        $db  = Artlable::find()->orderby('artlable_addtime desc')->asarray()->all();
        /*注意设置*/
		$notice = SysConfig::find()->where("config_type=20")->asarray()->all();
		return ['NewArticle'=>$NewArticle,'db'=>$db,'notice'=>$notice];
	}
	/**
	 * @inheritdoc 资讯首页
	 */
	public function actionIndex()
	{
		$pages = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => Article::find()->count(),
        ]);
        $model= new Article;
        $model = $model->Informations($pages);
        /*$info = $this->Commen();*/
        return $this->render('index',['artlist'=>$model,'pages'=>$pages]);
	}

	/**
	 * @inheritdoc 资讯标签搜索
	 */
	public function actionSearch()
	{
		$lable = Yii::$app->request->get("lable_id");
		$model= new Article;
		$pages = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $model->ArticleCount($lable),
        ]);
        $model = $model->InforSearch($pages,$lable);
        /*$info = $this->Commen();*/
        return $this->render('index',['artlist'=>$model,'pages'=>$pages]);
	}

	/**
	 * @inheritdoc 资讯详情页
	 */
	public function actionDetail()
	{
		$request = yii::$app->request->post();
		$article_id = yii::$app->request->get("article_id");
		$model = new ArticleInfo;
		$article= new Article;
	    $artlist = $article->Details($article_id);
	    /*上篇文章*/
	    $prev = $article->Prev($article_id);
	    /*下篇文章*/		
	    $next = new Article;
	    $next = $next->Next($article_id);
	    $release = Comment::find()
				->select("edu_comment.*,edu_article.article_id,edu_user.user_thumb")
				->innerjoin("edu_article","edu_comment.model_id=edu_article.article_id")
				->innerjoin("edu_user","edu_comment.user_id=edu_user.user_id")
				->where("edu_comment.model_id = $article_id")->orderby("edu_comment.comment_addtime desc")->limit(5)->asarray()->all();	
	    return $this->render('detail',[
	        			'model'=>$model,
				        'artlist'=>$artlist,
				        'prev'=>$prev,
				        'next'=>$next,
				        'release'=>$release
	        ]);
	}

	/**
	 * @inheritdoc 
	 */
	public function actionCount()
	{
		$article_id = yii::$app->request->post("article_id");
		$article= new Article;
        $artlist = $article->Details($article_id);
		$count = Article::findOne($article_id);
        $num = $artlist['article_count']+1;
        $count->article_count =$num;
        $count->save();
	}
	/**
	 * @inheritdoc  资讯评论
	 */
	public function actionComment()
	{
		$article_id = yii::$app->request->post("article_id");
		$session = Yii::$app->session;
		$user_id = $session->get('user_id');
		$db = User::find()->where(['user_id'=>$user_id])->asarray()->one();
        $user_name = $db['user_name'];
		$str = Yii::$app->request->post("comment_content");
		/*$sql = "SELECT find,replacement from edu_Illegal WHERE instr($str,find)";*/
		/*$str = "我估算UI妮哦AV12次下跪拉萨路54式空间里看见爱上的女1夜情";*/
		if(empty($str))
		{
			echo "<script>alert('内容不能为空');location.href='detail?article_id=$article_id'</script>";die;
		}
		$arr = new Comment;
		$arr = $arr->CommentAdd($str);
		foreach ($arr as $key => $val) 
		{
			$error = $val['find'];
			$replace = $val['replacement'];	
			$info = preg_replace("/$error/",$replace,$str);
			$str=$info;		
		}
		$model = new Comment;
		$model->comment_type 	= 1;
		$model->model_id     	= Yii::$app->request->post("article_id");
		$model->comment_addtime = time();
		$model->user_id			= $user_id;
		$model->user_name		= $user_name;
		$model->comment_content	= $str;
		$rs = $model->save();
		if($rs){	
			echo "<script>alert('评论成功');location.href='detail?article_id=$article_id'</script>";
		}else{
			echo "<script>alert('评论失败');location.href='detail?article_id=$article_id'</script>";
		}
	}

	/**
	 * @inheritdoc 收藏
	 */
	public function actionCollect()
	{
		$session = Yii::$app->session;
		$user_id = $session->get('user_id');
		$request = Yii::$app->request->get();
		$id = $request['id'];
		if($user_id){
			$collection = new Collection;
			$data = $collection -> collect($user_id,$id,1);
			if($data){
				return json_encode([
					'code' => 2,
					'msg'  => '您已收藏！',
				]);die;
			}else{
				$collection -> user_id = $user_id;
				$collection -> collect_type = 1;
				$collection -> model_id = $id;
				$collection -> collect_addtime = time();
				$res = $collection -> save();
				if($res){
					return json_encode([
						'code' => 3,
						'msg'  => '收藏成功',
					]);die;
				}else{
					return json_encode([
						'code' => 4,
						'msg'  => '收藏失败',
					]);die;
				}
			}
		}else{
			return json_encode([
					'code' => 1,
					'msg'  => '请先登录！',
				]);die;
		}
	}

	


}